<template>
  <div class="container">

    <div class="input-group" v-if="SearchState">
      <input type="text" class="input-field" v-model="SearchValue" placeholder="请输入内容..." />
      <button class="submit-btn" @click="submit">搜索</button>
    </div>

    <template v-if="route.params.id == 'person'">
      <h2>{{ route.params.id }} - {{ "类型" }}</h2>

      <div class="filter">
        <div>
          <label for="filter1">筛选条件1 </label>
          <select id="filter1" v-model="selectedFilter1">
            <option value="">所有</option>
            <option v-for="(condition, index) in conditions" :key="index" :value="condition">{{ condition }}</option>
          </select>
        </div>

        <div>
          <label for="filter2">筛选条件2 </label>
          <select id="filter2" v-model="selectedFilter2">
            <option value="">所有</option>
            <option v-for="(condition, index) in conditions" :key="index" :value="condition">{{ condition }}</option>
          </select>
        </div>

        <div>
          <label for="filter3">筛选条件3 </label>
          <select id="filter4" v-model="selectedFilter3">
            <option value="">所有</option>
            <option v-for="(condition, index) in conditions" :key="index" :value="condition">{{ condition }}</option>
          </select>
        </div>

        <div>
          <label for="filter4">筛选条件4 </label>
          <select id="filter4" v-model="selectedFilter4">
            <option value="">所有</option>
            <option v-for="(condition, index) in conditions" :key="index" :value="condition">{{ condition }}</option>
          </select>
        </div>
      </div>

      <div class="card">
        <div class="grid-container">
          <div class="figure" v-for="item in 20" @click="onheadLink(item)">
            <img src="/logo.png" alt="User Avatar" class="figure-avatar" />
            <div class="figure-info">
              <div class="figure-time">2023-04-01</div>
              <div class="figure-quantity">521</div>
            </div>
          </div>
        </div>
      </div>

      <div class="pagination">
        <button @click="previousPage" v-if="currentPage != 1">上一页</button>
        <!-- <span>第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span> -->
        <input type="text" v-model="currentPage" min="1" :max="totalPages" @keyup.enter="jumpToPage" />
        <span>/ {{ totalPages }} </span>
        <button @click="nextPage" v-if="currentPage != totalPages">下一页</button>
      </div>
    </template>

    <template v-if="route.params.id == 'type'">
      <h2>{{ route.params.id }} - {{ "类型" }}</h2>

      <div class="card">
        <div class="grid-container2">
          <div class="item" v-for="(item, index) in items" :key="index" @click="onheadLink(item.quantity)">
            <div class="item-type">{{ item.type }}</div>
            <div class="item-quantity">{{ item.quantity }}</div>
          </div>
        </div>
      </div>

      <div class="pagination">
        <button @click="previousPage" v-if="currentPage != 1">上一页</button>
        <!-- <span>第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span> -->
        <input type="text" v-model="currentPage" min="1" :max="totalPages" @keyup.enter="jumpToPage" />
        <span>/ {{ totalPages }} </span>
        <button @click="nextPage" v-if="currentPage != totalPages">下一页</button>
      </div>
    </template>

    <template v-if="route.params.id == 'family'">
      <h2>{{ route.params.id }} - {{ "类型" }}</h2>

      <div class="card">
        <div class="grid-container2">
          <div class="item" v-for="(item, index) in items" :key="index" @click="onheadLink(item.quantity)">
            <div class="item-type">{{ item.type }}</div>
            <div class="item-quantity">{{ item.quantity }}</div>
          </div>
        </div>
      </div>

      <div class="pagination">
        <button @click="previousPage" v-if="currentPage != 1">上一页</button>
        <!-- <span>第 {{ currentPage }} 页 / 共 {{ totalPages }} 页</span> -->
        <input type="text" v-model="currentPage" min="1" :max="totalPages" @keyup.enter="jumpToPage" />
        <span>/ {{ totalPages }} </span>
        <button @click="nextPage" v-if="currentPage != totalPages">下一页</button>
      </div>
    </template>
  </div>
</template>

<script setup lang="ts">
const route = useRoute();
const id = route.params.id;

const selectedFilter1 = ref("");
const selectedFilter2 = ref("");
const selectedFilter3 = ref("");
const selectedFilter4 = ref("");

const conditions = ref(["条件1-1", "条件1-2", "条件1-3", "条件1-4"]);

const currentPage = ref<any>(1); // 当前页码
const totalPages = ref(10); // 总页数，这个值应根据实际情况动态获取或计算

const previousPage = () => {
  if (currentPage.value > 1) {
    currentPage.value--;
  }
};

const nextPage = () => {
  if (currentPage.value < totalPages.value) {
    currentPage.value++;
  }
};

const jumpToPage = () => {
  // 获取用户输入的页码
  const pageNumber = parseInt(currentPage.value);

  // 验证输入是否为数字且在有效范围内
  if (!isNaN(pageNumber) && pageNumber >= 1 && pageNumber <= totalPages.value) {
    // 跳转到用户输入的页码
    currentPage.value = pageNumber;
  } else {
    // 如果输入无效，可以给出提示或者重置为当前页
    alert("请输入有效的页码！");
    // 这里可以选择不进行任何操作保持当前页，或者重置currentPage的值
  }
};

const router = useRouter();

const onheadLink = (item: any) => {
  router.push({ path: `/grouping/${item}` });
};

const items = ref([
  { type: "电子产品", quantity: 25 },
  { type: "衣物", quantity: 18 },
  { type: "书籍", quantity: 12 },
  { type: "电子产品", quantity: 25 },
  { type: "衣物", quantity: 18 },
  { type: "书籍", quantity: 12 },
  { type: "电子产品", quantity: 25 },
  { type: "衣物", quantity: 18 },
  { type: "书籍", quantity: 12 },
  { type: "电子产品", quantity: 25 },
]);


const SearchState: any = useSearch();

const SearchValue = ref("");

const submit = () => {
  console.log(SearchValue.value);
  router.push({ path: `/search/${SearchValue.value}` });
};
</script>

<style scoped>
.container {
  width: 1600px;
  margin: 20px auto;
  max-width: 100%;
  box-sizing: border-box;
}

.card {
  width: 1600px;
  padding: 20px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  border-radius: 5px;
  text-align: center;
  background-color: #ffffff;
}

/* .card div {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  height: 50px;
  justify-content: space-around;
}

.card div span:first-child {
  color: #e6a23c;
}
.card div span:last-child {
  color: #909399;
} */

h2 {
  text-align: center;
}

.filter {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 20px;
}

.filter select {
  appearance: none; /* 移除默认样式，适用于大多数浏览器 */
  -moz-appearance: none; /* Firefox */
  -webkit-appearance: none; /* Safari 和 Chrome */
  background-color: #f8f8f8;
  border: 1px solid #ddd;
  padding: 5px 10px;
  border-radius: 5px;
  font-size: 14px;
  cursor: pointer;
  outline: none;
  width: 170px; /* 您可以根据需要调整宽度 */
}

.filter select:focus {
  border-color: #5bc0de;
}

/* 自定义下拉箭头 */
.filter select::after {
  content: "\25BC"; /* 下三角符号 */
  position: absolute;
  top: 50%;
  right: 10px;
  transform: translateY(-50%);
  pointer-events: none; /* 确保点击箭头时仍能选择选项 */
  color: #999;
}

.figure {
  display: flex;
  align-items: center;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}

.figure:hover {
  cursor: pointer;
  background-color: rgba(0, 0, 0, 0.1);
}

.figure-avatar {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  margin-right: 10px;
}

.figure-info {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  height: 100px;
  justify-content: space-around;
}

.figure-time {
  color: #666;
  margin-bottom: 5px;
  font-size: 24px;
}

.figure-quantity {
  font-weight: bold;
  font-size: 24px;
}

.grid-container {
  display: grid;
  grid-template-columns: repeat(4, 1fr); /* 这里定义了4列，每列宽度相等 */
  grid-gap: 20px; /* 设置网格之间的间隔，可根据需要调整 */
}

.grid-container2 {
  display: grid;
  grid-template-columns: repeat(8, 1fr); /* 这里定义了4列，每列宽度相等 */
  grid-gap: 20px; /* 设置网格之间的间隔，可根据需要调整 */
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  margin-top: 20px;
}

.pagination button {
  background-color: #4caf50;
  border: none;
  color: white;
  padding: 8px 16px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
  border-radius: 5px;
}

.pagination button[disabled] {
  opacity: 0.6;
  cursor: not-allowed;
}

.pagination span {
  font-weight: bold;
  color: #333;
}

.pagination input[type="text"] {
  /* 设置基本样式 */
  width: 40px; /* 调整宽度以适应你的布局 */
  padding: 5px 10px; /* 内边距，使得文本居中且有舒适的点击区域 */
  margin: 0 5px; /* 与其他元素保持适当距离 */
  text-align: center; /* 文本居中 */
  border: 1px solid #ccc; /* 边框样式 */
  border-radius: 4px; /* 边框圆角 */
  font-size: 14px; /* 字体大小 */

  /* 更美观的悬停和聚焦状态 */
  transition: border-color 0.3s ease;
}

.pagination input[type="text"]:hover,
.pagination input[type="text"]:focus {
  border-color: #4caf50; /* 鼠标悬停或聚焦时边框颜色改变 */
  outline: none; /* 移除默认的轮廓，若不需要 */
}

/* 可选：统一输入框与按钮的样式，例如颜色和字体 */
.pagination input[type="text"],
.pagination button {
  color: #333;
  font-family: Arial, sans-serif; /* 或其他字体 */
}

/* 可选：增加一些反馈效果，比如当输入无效时的样式 */
.pagination input[type="text"].invalid {
  border-color: #f44336; /* 红色边框表示错误 */
}

.item {
  display: flex;
  flex-direction: column;
  height: 50px;
  justify-content: space-around;
  padding: 10px;
  font-size: 18px;

  border: 1px solid #ddd;
  border-radius: 5px;
}

.item:hover {
  cursor: pointer;
  background-color: rgba(0, 0, 0, 0.1);
}




.input-group {
  position: relative;
  width: 700px;
  margin: 20px auto;
}

.input-field {
  width: 600px;
  padding: 15px 20px;
  border-radius: 25px;
  border: 1px solid #ddd;
  outline: none;
  box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.05);
  font-size: 14px;
  transition: all 0.3s ease;
}

.input-field:focus {
  border-color: #007bff;
  box-shadow: 0 0 5px rgba(0, 123, 255, 0.3);
}

/* 按钮样式 */
.submit-btn {
  position: absolute;
  right: 0;
  top: 0;
  width: 100px;
  height: 100%;
  padding: 0 20px;
  border: none;
  outline: none;
  background-color: #007bff;
  color: white;
  cursor: pointer;
  border-radius: 0 25px 25px 0;
  font-size: 14px;
  transition: all 0.3s ease;
}

.submit-btn:hover {
  background-color: #0056b3;
}

/* 输入框聚焦时按钮效果 */
.input-field:focus + .submit-btn {
  background-color: #0056b3;
}
</style>
